#ifndef GAME_LOGIC_H
#define GAME_LOGIC_H

#define SAFE_DELETE(p) {if(p) {delete(p);(p)=NULL;}}

#include <string>
#include <iostream>

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

#include "grid.h"
#include "piece.h"
#include "t_block.h"
#include "rl_block.h"
#include "l_block.h"
#include "line_block.h"
#include "square_block.h"
#include "z_block.h"
#include "s_block.h"
#include <ctime>
#include "Game_view.h"

using namespace std;

class Game_Logic
{
    //Possible game estates
    enum {GS_INITIALIZING, GS_RUNNING, GS_GAMEOVER, GS_WAIT};

    //Events we can handle
    enum {BLOCK_MOVE, TABLE_UPDATE, GS_CHANGE, NEW_PIECE};

    //Possible movements
    enum {LEFT, RIGHT, DOWN};

    public:
        Game_Logic(Game_view* gv);
        virtual ~Game_Logic();
        bool Update();
        Game_view* gameV;
        void UpdateGrid(int update_type);

    protected:
    private:

        //private methods
        bool HandleEvents();
        void Copy_piece();
        piece* Random_piece();
        bool Check_new_position(int mov_x, int mov_y);


        //private variables
        int gamestate;
        grid* mp_game_table;
        piece* current_piece;
        piece* next_piece;
        Uint32 timers[3]; //timers used for piece movements
        SDL_TimerID piece_down_timer;
};

#endif // GAME_LOGIC_H
